package com.example.ssm.controller;

import com.example.ssm.entity.Bill;
import com.example.ssm.entity.Menu;
import com.example.ssm.entity.Merchant;
import com.example.ssm.entity.User;
import com.example.ssm.service.BillService;
import com.example.ssm.service.MenuService;
import com.example.ssm.service.ShopService;
import com.example.ssm.service.UserService;
import com.example.ssm.service.impl.BillServiceImpl;
import com.example.ssm.service.impl.MenuServiceImpl;
import com.example.ssm.service.impl.ShopServiceImpl;
import com.example.ssm.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {
    @RequestMapping("/addBill")
    public String addBill(Bill bill, HttpServletRequest req, String user_name, Model model) throws IOException {
        BillService billService=new BillServiceImpl();
        billService.insertBill(bill);
        User user=new User();
        user.setUname(user_name);


        MenuService menuService=new MenuServiceImpl();


        List<Menu> menus= menuService.selectAllMenu();
        menus=menuService.selectAllMenu();
        req.setAttribute("list",menus);
        model.addAttribute("menus",menus);

        List<Bill>NOPayBill= billService.selectNoPayByUserName(user);
        List<Bill>PayedBill= billService.selectPayByUserName(user);
        model.addAttribute("NOPayBill",NOPayBill);
        model.addAttribute("PayedBill",PayedBill);
        double value=0;
        int j=0;
        while (j<NOPayBill.size() ){
            double price= NOPayBill.get(j).getPrice();
            value+=price;
            j++;
        }
        req.setAttribute("value",value);

        MenuService service=new MenuServiceImpl();
        service.selectMenuById(bill);
        return "shophome";
    }
    @RequestMapping("/delBill")
    public String delBill(Bill bill,HttpServletRequest req,String userName,Model model) throws IOException {
        BillService billService=new BillServiceImpl();
        billService.delNoPayById(bill);
        User user=new User();
        user.setUname(userName);


        MenuService menuService=new MenuServiceImpl();


        List<Menu>menus;
        menus=menuService.selectAllMenu();
        req.setAttribute("list",menus);
        model.addAttribute("menus",menus);

        List<Bill> NOPayBill= billService.selectNoPayByUserName(user);
        List<Bill> PayedBill= billService.selectPayByUserName(user);
        model.addAttribute("NOPayBill",NOPayBill);
        req.setAttribute("2",NOPayBill);
        model.addAttribute("PayedBill",PayedBill);
        double value=0;
        int j=0;
        while (j<NOPayBill.size() ){
            double price= NOPayBill.get(j).getPrice();
            value+=price;
            j++;
        }
        model.addAttribute("value",value);

        double value1=0;
        int k=0;
        while (j<PayedBill.size() ){
            double price= PayedBill.get(k).getPrice();
            value1+=price;
            k++;
        }
        model.addAttribute("value1",value1);


        return "shophome";
    }
    @RequestMapping("/payBill")
    public String payBill(HttpServletRequest req, HttpSession session, String user_name, Model model, double value) throws IOException {
        UserService userService=new UserServiceImpl();
        User user=new User();
        User user1=new User();
        user.setUname(user_name);
        user1=userService.selectByName(user);



        if(user1.getAccount()<value){


            session.setAttribute("USER",user1);
            MenuService menuService=new MenuServiceImpl();
            BillService billService=new BillServiceImpl();

            List<Menu>menus= menuService.selectAllMenu();
            menus=menuService.selectAllMenu();
            req.setAttribute("list",menus);
            model.addAttribute("menus",menus);

            List<Bill> NOPayBill= new ArrayList<Bill>();
            NOPayBill=  billService.selectNoPayByUserName(user);
            List<Bill> PayedBill= new ArrayList<Bill>();
            PayedBill= billService.selectPayByUserName(user);
            double value2=0;
            int j=0;
            while (j<NOPayBill.size() ){
                double price= NOPayBill.get(j).getPrice();
                value2+=price;
                j++;
            }
            model.addAttribute("value",value2);

            double value1=0;
            int k=0;
            while (k<PayedBill.size() ){
                double price= PayedBill.get(k).getPrice();
                value1+=price;
                k++;
            }
            model.addAttribute("value1",value1);
            model.addAttribute("NOPayBill",NOPayBill);
            model.addAttribute("PayedBill",PayedBill);
            model.addAttribute("noM","账户余额不足");
            return "shophome";
        }else{
            double account=user1.getAccount()-value;
            user1.setAccount(account);
            userService.addMoney(user1);
            BillService billService=new BillServiceImpl();
            MenuService menuService=new MenuServiceImpl();



            List<Bill>bills= billService.selectNoPayByUserName(user);
            for (int l=0;l<bills.size();l++){
                System.out.println(bills.get(l));
                Merchant merchant=new Merchant();
                Bill bill=new Bill();
                bill.setPrice(bills.get(l).getPrice());
                merchant.setShop_name(bills.get(l).getShop_name());
                ShopService shopService=new ShopServiceImpl();
                Merchant merchant1=new Merchant();
                merchant1=shopService.selectByShopName(merchant);
                double money;
                money=merchant1.getAccount()+bill.getPrice();
                User user2=new User();
                user2.setUname(merchant1.getUser_name());
                user2.setAccount(money);
                userService.addMoney(user2);
                merchant1.setAccount(money);
                shopService.addMoney(merchant1);

            }
            User user3=new User();
            user3.setUname(user_name);

            billService.updateBill(user3);
            User user2=new User();

            user2=userService.selectByName(user3);
            session.setAttribute("USER",user2);
            req.setAttribute("USER",user2);

            List<Menu>menus= menuService.selectAllMenu();
            menus=menuService.selectAllMenu();
            req.setAttribute("list",menus);
            model.addAttribute("menus",menus);

            List<Bill>NOPayBill= billService.selectNoPayByUserName(user);
            List<Bill>PayedBill= billService.selectPayByUserName(user);
            model.addAttribute("NOPayBill",NOPayBill);
            model.addAttribute("PayedBill",PayedBill);
            double value2=0;
            int j=0;
            while (j<NOPayBill.size() ){
                double price= NOPayBill.get(j).getPrice();
                value2+=price;
                j++;
            }
            model.addAttribute("value",value2);
            double value1=0;
            int k=0;
            while (k<PayedBill.size() ){
                double price= PayedBill.get(k).getPrice();
                value1+=price;
                k++;
            }
            model.addAttribute("value1",value1);
            return "shophome";
        }

    }

}
